package com.mallcai.bigdata.ladon.dpl.mapper.peekdata;

import com.mallcai.bigdata.ladon.dpl.entity.peekdata.Model;
import com.mallcai.bigdata.ladon.dpl.mapper.peekdata.provider.ModelSqlProvider;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

import static com.mallcai.bigdata.ladon.dpl.mapper.peekdata.provider.ModelSqlProvider.TB_MODEL;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
@Repository
public interface ModelMapper {

    @InsertProvider(type = ModelSqlProvider.class, method = "insert")
    public int insert(Model model);

    @UpdateProvider(type = ModelSqlProvider.class, method = "update")
    public int update(Model model);

    @Update({
            "UPDATE " + TB_MODEL + " SET archive = 1, modified = unix_timestamp(now()), modified_by=#{modifiedBy} WHERE id = #{id}"
    })
    public int delete(Model model);

    @Select({
            "SELECT * FROM " + TB_MODEL + " WHERE id = #{id}"
    })
    public Model getById(@Param("id") Long id);

    @SelectProvider(type = ModelSqlProvider.class, method = "getByParams")
    public List<Model> getByParams(@Param("params") Map<String, Object> params);

    @Select({
            "SELECT count(0) FROM " + TB_MODEL + " WHERE archive = 0 AND datasource_id = #{datasourceId}"
    })
    public int getSizeOfDatasource(@Param("datasourceId") Long datasourceId);

    @Select({
            "SELECT * FROM " + TB_MODEL + " WHERE archive = 0 AND name = #{name}"
    })
    public List<Model> getByName(@Param("name") String name);

    @Select({
            "SELECT * FROM " + TB_MODEL + " WHERE archive = 0 AND  status = 0"
    })
    public List<Model> getAllModels();
}
